/**
 * 版权所有：厦门市巨龙信息科技有限公司
 * Copyright 2023 Xiamen Dragoninfo Eng. Co. Ltd.
 * All right reserved.
 */
package com.github.alfred.tool.impl;

import com.github.alfred.tool.DistributedLocker;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 类说明
 *
 * @author chengh
 * @date 2023/5/10 16:39
 */
@Component
public class RedissonDistributedLocker implements DistributedLocker {
    private static final Logger log = LoggerFactory.getLogger(RedissonDistributedLocker.class);
    @Autowired
    private RedissonClient redissonClient;

    @Override
    public RLock lock(String lockKey) {
        RLock lock = this.redissonClient.getLock(lockKey);
        lock.lock();
        return lock;
    }

    @Override
    public RLock lock(String lockKey, long timeout) {
        RLock lock = this.redissonClient.getLock(lockKey);
        //支持过期解锁功能,10秒钟以后自动解锁, 无需调用unlock方法手动解锁
        lock.lock(timeout, TimeUnit.SECONDS);
        return lock;
    }

    @Override
    public RLock lock(String lockKey, TimeUnit unit, long timeout) {
        RLock lock = this.redissonClient.getLock(lockKey);
        lock.lock(timeout, unit);
        return lock;
    }

    @Override
    public RLock tryLock(String lockKey) {
        RLock lock = this.redissonClient.getLock(lockKey);
        boolean isLock = lock.tryLock();
        while (!isLock) {
            log.error("等待{}锁释放", lockKey);
            isLock = lock.tryLock();
        }
        return lock;
    }

    @Override
    public boolean tryLock(String lockKey, TimeUnit unit, long leaseTime) {
        RLock lock = this.redissonClient.getLock(lockKey);
        try {
            return lock.tryLock(0L, leaseTime, unit);
        } catch (InterruptedException var7) {
            log.error(var7.getMessage(), var7);
            return false;
        }
    }

    @Override
    public boolean tryLock(String lockKey, TimeUnit unit, long waitTime, long leaseTime) {
        RLock lock = redissonClient.getLock(lockKey);
        try {
            return lock.tryLock(waitTime, leaseTime, unit);
        } catch (InterruptedException var7) {
            log.error(var7.getMessage(), var7);
            return false;
        }
    }

    @Override
    public void unlock(String lockKey) {
        RLock lock = redissonClient.getLock(lockKey);
        try {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        } catch (IllegalMonitorStateException var7) {
            log.error(var7.getMessage(), var7);
        }
    }

    @Override
    public void unlock(RLock lock) {
        try {
            if (lock == null) {
                return;
            }
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        } catch (IllegalMonitorStateException var3) {
            log.error(var3.getMessage(), var3);
        }
    }


}
